use std::fs::File;
use std::io::BufWriter;
use std::io::prelude::*;

#[derive(Debug, Clone, Copy)]
pub struct Color {
    r: u8,
    g: u8,
    b: u8,
}

impl Color {
    pub fn new() -> Color {
        Color {
            r: 0,
            g: 0,
            b: 0
        }
    }

    pub fn RGB(r: u8, g: u8, b: u8) -> Color {
        Color {
            r,g,b
        }
    }
}

pub struct Image {
    pub width: u32,         //
    pub height: u32, //
    pub pixels: Vec<Vec<Color>>, //
}

impl Image {
    pub fn new(width: u32, height: u32) -> Self {
        Image {
            width,
            height,
            pixels: vec![vec![Color::new(); width as usize]; height as usize],
        }
    }

    pub fn to_ppm(&self) {
        let file = File::create("out.ppm").unwrap();
        let mut buffer = BufWriter::new(file);

        write!(buffer, "P6\n{} {}\n255\n", self.width, self.height);
        for row in 0..self.height {
            for col in 0..self.width {
                let pixel = &self.pixels[row as usize][col as usize];
                buffer.write(&[pixel.r, pixel.g, pixel.b]);
            }
        }
    }
}
